package com.ken.service.impl;

import com.ken.constant.StatusConstant;
import com.ken.entity.Orders;
import com.ken.entity.User;
import com.ken.mapper.DishMapper;
import com.ken.mapper.OrderMapper;
import com.ken.mapper.SetmealMapper;
import com.ken.mapper.UserMapper;
import com.ken.service.OrderService;
import com.ken.service.UserService;
import com.ken.service.WorkspaceService;
import com.ken.vo.BusinessDataVO;
import com.ken.vo.DishOverViewVO;
import com.ken.vo.OrderOverViewVO;
import com.ken.vo.SetmealOverViewVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.ken.entity.Orders.*;

@Service
@Slf4j
public class WorkspaceServiceImpl implements WorkspaceService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderService orderService;
    @Autowired
    private UserService userService;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 根据时间段统计营业数据
     * @param begin
     * @param end
     * @return
     */
    public BusinessDataVO getBusinessData(LocalDateTime begin, LocalDateTime end) {
        /**
         * 营业额：当日已完成订单的总金额
         * 有效订单：当日已完成订单的数量
         * 订单完成率：有效订单数 / 总订单数
         * 平均客单价：营业额 / 有效订单数
         * 新增用户：当日新增用户的数量
         */
        begin=LocalDateTime.MIN;
        end=LocalDateTime.MAX;
        //查询总订单数
        //查询每天订单总数
        int totalOrderCount = orderService.lambdaQuery()
                .ge(Orders::getOrderTime, begin)
                .le(Orders::getOrderTime, end).count().intValue();
        //营业额

        List<Orders> orderListByDay = orderService.lambdaQuery()
                .eq(Orders::getStatus, COMPLETED)
                .ge(Orders::getOrderTime, begin)
                .le(Orders::getOrderTime, end)
                .list();
        double dailyEarn = orderListByDay
                .stream()
                .map(Orders::getAmount)
                .reduce(BigDecimal.ZERO, BigDecimal::add)
                .doubleValue();
        //有效订单数
        int validOrderCount = orderService.lambdaQuery()
                .ge(Orders::getOrderTime, begin)
                .le(Orders::getOrderTime, end)
                .eq(Orders::getStatus, COMPLETED).count().intValue();

        Double unitPrice = 0.0;

        Double orderCompletionRate = 0.0;
        if(totalOrderCount != 0 && validOrderCount != 0){
            //订单完成率
            orderCompletionRate = (double) (validOrderCount / totalOrderCount);
            //平均客单价
            unitPrice = dailyEarn / validOrderCount;
        }

        //新增用户数
        int newUsers = userService.lambdaQuery()
                .ge(User::getCreateTime, begin)
                .le(User::getCreateTime, end).list().size();


        return BusinessDataVO.builder()
                .turnover(dailyEarn)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .newUsers(newUsers)
                .build();
    }


    /**
     * 查询订单管理数据
     *
     * @return
     */
    public OrderOverViewVO getOrderOverView() {


        //待接单
        int waitingOrders=orderService.lambdaQuery().eq(Orders::getStatus,TO_BE_CONFIRMED).count().intValue();

        //待派送
        int deliveredOrders=orderService.lambdaQuery().eq(Orders::getStatus,CONFIRMED).count().intValue();

        //已完成
        int completedOrders=orderService.lambdaQuery().eq(Orders::getStatus,COMPLETED).count().intValue();

        //已取消
        int cancelledOrders=orderService.lambdaQuery().eq(Orders::getStatus,CANCELLED).count().intValue();
        //全部订单
        int allOrders= orderService.lambdaQuery().count().intValue();

        return OrderOverViewVO.builder()
                .waitingOrders(waitingOrders)
                .deliveredOrders(deliveredOrders)
                .completedOrders(completedOrders)
                .cancelledOrders(cancelledOrders)
                .allOrders(allOrders)
                .build();
    }

    /**
     * 查询菜品总览
     *
     * @return
     */
    public DishOverViewVO getDishOverView() {
        Map map = new HashMap();
        map.put("status", StatusConstant.ENABLE);
        Integer sold = dishMapper.countByMap(map);

        map.put("status", StatusConstant.DISABLE);
        Integer discontinued = dishMapper.countByMap(map);

        return DishOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    /**
     * 查询套餐总览
     *
     * @return
     */
    public SetmealOverViewVO getSetmealOverView() {
        Map map = new HashMap();
        map.put("status", StatusConstant.ENABLE);
        Integer sold = setmealMapper.countByMap(map);

        map.put("status", StatusConstant.DISABLE);
        Integer discontinued = setmealMapper.countByMap(map);

        return SetmealOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }
}
